//
//  SellerSettingViewController.m
//  SmartCoin
//
//  Created by apple on 2018/3/8.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "SellerSettingViewController.h"
#import "SellerSettingTableViewCell.h"
#import "AboutUSViewController.h"
#import <JPUSHService.h>
#import <ShareSDKExtension/ShareSDK+Extension.h>
static NSString *SellerSettingTableViewCellIndentifier = @"SellerSettingTableViewCell.h";
@interface SellerSettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *mainView;
@property (nonatomic ,retain)NSArray *operations;
@property (nonatomic ,retain)UILabel *versionlabel;
@end

@implementation SellerSettingViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (cx_hit == 812) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
    else{
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    };
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (cx_hit == 812) {
        
    }
    else{
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _operations = @[@[@"关于我们",@"icon_about"],@[@"清除缓存",@"icon_clear"]];
    self.view.backgroundColor = bggrayColor;
    [self viewLoad];
}

-(void)viewLoad{
    [self setNavigationViewWithTitle:@"设置" WithCanPop:YES WithPopData:@"black_return" WithPopisString:NO WithHasRightOperation:NO WithrightOperationData:@"" WithRightisString:NO WithNavigationColor:[UIColor clearColor] withBgView:self.view];
    self.centerTitle.textColor = [UIColor blackColor];
    [self mainviewLoad];
}

-(void)mainviewLoad{
    if (!_mainView) {
        _mainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis, cx_wid, cx_hit - cxsingler.topdis - 44 - cxsingler.bottomdis)];
        _mainView.estimatedRowHeight = 0;
        _mainView.delegate = self;
        _mainView.dataSource = self;
        _mainView.scrollEnabled = NO;
        _mainView.backgroundColor = [UIColor clearColor];
        _mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_mainView registerClass:[SellerSettingTableViewCell class] forCellReuseIdentifier:SellerSettingTableViewCellIndentifier];
        [self.view addSubview:_mainView];
    }
    
    UIButton *quitBtn = [[UIButton alloc] initWithFrame:CGRectMake(12, cx_hit - 40 - cxsingler.bottomdis - 44, cx_wid - 24, 44)];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = quitBtn.bounds;
    layer.cornerRadius = 22;
    layer.colors = @[(id)[CXSinglerModel colorWithHex:@"#FF7F5A" alpha:1].CGColor,(id)[CXSinglerModel colorWithHex:@"#FE4438" alpha:1].CGColor];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(1, 0);
    [quitBtn.layer addSublayer:layer];
    [quitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [quitBtn addTarget:self action:@selector(quited:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitBtn];
    
    _versionlabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _versionlabel.font = [UIFont systemFontOfSize:14];
    _versionlabel.textAlignment = NSTextAlignmentCenter;
    _versionlabel.textColor = [UIColor blackColor];
    [self.view addSubview:_versionlabel];
    _versionlabel.text = [NSString stringWithFormat:@"version: %@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    if ([ShareSDK isClientInstalled:SSDKPlatformTypeWechat]) {//当前手机安装有微信
        _versionlabel.hidden = NO;
    }
    else{
        _versionlabel.hidden = YES;
    }
    [_versionlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-13 - cxsingler.bottomdis);
        make.height.mas_equalTo(14);
        make.width.mas_equalTo(cx_wid - 70);
    }];
}

-(void)quited:(UIButton *)sender{
    [JPUSHService setAlias:@"quitcode" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
        
        if (iResCode != 0) {
            NSLog(@"---Alex---:\n极光别名注册失败");
        }
    } seq:111111];
    cxsingler.personData = [NSMutableDictionary dictionary];
    cxsingler.loginrequest = [NSMutableDictionary dictionary];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark--uitableview代理开始
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _operations.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    SellerSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SellerSettingTableViewCellIndentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (row == 1) {
        cell.restlabel.text = [NSString stringWithFormat:@"%.2fM",[Utils filePath]];
    }
    cell.icon.image = [UIImage imageNamed:_operations[row][1]];
    cell.operationlabel.text = _operations[row][0];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    NSInteger sec = indexPath.section;
    if (row == 0) {
        AboutUSViewController *nextVC = [[AboutUSViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    if (row == 1) {
        NSString *word = [NSString stringWithFormat:@"确定要清除%.2fM缓存吗?",[Utils filePath]];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:word preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [Utils removeCache];
            [self.mainView reloadData];
            [SVProgressHUD showSuccessWithStatus:@"缓存清理成功"];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:cancel];
        [alert addAction:sure];
        dispatch_async(dispatch_get_main_queue(), ^{
           [self presentViewController:alert animated:NO completion:nil];
        });
    }
}
#pragma mark--uiatbelview代理结束

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
